﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SyncLib.Interface;
using Microsoft.Synchronization.Data;
using Microsoft.Synchronization;
using System.Data.SqlClient;
using System.Data;
using log4net;
using Microsoft.Synchronization.Data.SqlServer;
using SyncLib.Data;

namespace SyncLib.Provider
{
    public class SimpleSync : BaseKeeperSync
    {
        ILog Log = LogManager.GetLogger(typeof(Log4NetImplement));
        
        #region Override Method
        protected override void OnSyncProgress(DbSyncProgressEventArgs e)
        {
            //base.OnSyncProgress(e);
            string step = "0";
            try
            {
                Log.Debug(
                         string.Format("[SyncProgress] SessionId={0} ScopeName {1} : State={2} Table ={3} Rowindex={4}/RowCount={5}",
                         SessionId, Context.CurrentScope.ScopeName, e.Stage, e.TableProgress.TableName, e.TableProgress.RowIndex,
                         e.TableProgress.DataTable.Rows.Count));
                //if (e.TableProgress.RowIndex % 20 == 0)
                //    KeepAlive();
                switch (e.Stage)
                {

                    case DbSyncStage.ApplyingDeletes:
                    case DbSyncStage.ApplyingInserts:
                    case DbSyncStage.ApplyingUpdates:
                        {
                            if (e.TableProgress.DataTable.Rows.Count > e.TableProgress.RowIndex)
                            {
                                DataView view = new DataView(e.TableProgress.DataTable);
                                view.RowStateFilter = DataViewRowState.CurrentRows | DataViewRowState.Deleted;
                                step = "1";

                                // DataRowView rowView = e.TableProgress.DataTable.DefaultView[e.TableProgress.RowIndex];
                                DataRowView rowView = view[e.TableProgress.RowIndex];
                                step = "2";
                                // if (!rowView.Row.Equals(e.TableProgress.DataTable.Rows[e.TableProgress.RowIndex]))
                                if (!rowView.Row.Equals(e.TableProgress.DataTable.Rows[e.TableProgress.RowIndex]))
                                {
                                    step = "3";
                                    Log.Info("RowIndex not match dataview--> Manunal localtion rowview");
                                    rowView = null;
                                    // foreach (DataRowView rowv in e.TableProgress.DataTable.DefaultView)
                                    foreach (DataRowView rowv in view)
                                        if (rowv.Row.Equals(e.TableProgress.DataTable.Rows[e.TableProgress.RowIndex]))
                                        {
                                            rowView = rowv;
                                            break;
                                        }
                                }

                                TableOfScope tos = _SyncContext.CurrentScope.FindTable(e.TableProgress.TableName);
                                KeeperSyncUtility.CheckParameterNull(tos, "TableOfScope",
                                    string.Format("Không tìm thấy {0} trong phạm vi {1}",
                                    e.TableProgress.TableName,
                                    _SyncContext.CurrentScope.ScopeName));

                                if (!string.IsNullOrEmpty(tos.AfterApplyCommand))
                                {
                                    _SyncContext.CurrentTable = tos;
                                    KeeperSyncUtility.TOSAfterApplyCommand(rowView, e.Stage, this);
                                }
                            }
                        }
                        break;
                }
            }
            catch (Exception ex)
            {
                Log.Fatal(
                   string.Format(" [SyncProgress] SessionId={0} ScopeName={1} State={2} Table ={3} Rowindex={4}",
                   SessionId, Context.CurrentScope.ScopeName, e.Stage, e.TableProgress.TableName, e.TableProgress.RowIndex),
                       ex);
                throw ex;
            }
            finally
            {
                _SyncContext.CurrentTable = null;
                _SyncContext.CurrentDepen = null;
            }
        }
        
        #endregion

    }
}
